package revolucion.window;

import revolucion.database.Database;
import revolucion.classes.Log;
import revolucion.classes.User;

import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

public class Window_PassDialog extends JFrame {
  String session;
  User user;

  public Window_PassDialog(String _session, User _user) {
    initComponents();

    setIconImage(new ImageIcon(getClass().getResource("/revolucion/resources/admin.gif")).getImage());

    this.session = _session;
    this.user = _user;

    Dimension dim = this.getToolkit().getScreenSize();
    int x = (int) (dim.getWidth()  - this.getSize().getWidth() ) / 2;
    int y = (int) (dim.getHeight() - this.getSize().getHeight()) / 2;
    this.setLocation(x,y);
  }
    
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    btn_login = new javax.swing.JButton();
    txt_pass = new javax.swing.JPasswordField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);

    jLabel1.setText("Insira a senha para esta sessão");
    jLabel1.setName("jLabel1"); // NOI18N

    btn_login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/revolucion/resources/check.gif"))); // NOI18N
    btn_login.setName("btn_login"); // NOI18N
    btn_login.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btn_loginActionPerformed(evt);
      }
    });

    txt_pass.setName("txt_pass"); // NOI18N
    txt_pass.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        txt_passKeyPressed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(39, 39, 39)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(jLabel1)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(txt_pass)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btn_login, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap(24, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(btn_login)
          .addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_loginActionPerformed
      if(session.equals("logs")) {
        if(Database.checkPass(session, txt_pass.getText())){
          Window_Logs w = new Window_Logs();
          w.setVisible(true);
          this.dispose();
        } else {
          JOptionPane.showMessageDialog( this, "Senha incorreta!" );
          Log.writeLog(user.getUsername(), "Usuário " + user.getUsername() + " tentou acessar logs com senha "+ txt_pass.getText() +".", "");
        }
      }
    }//GEN-LAST:event_btn_loginActionPerformed

    private void txt_passKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_passKeyPressed
      if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
        btn_login.doClick();
      }
    }//GEN-LAST:event_txt_passKeyPressed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  public javax.swing.JButton btn_login;
  private javax.swing.JLabel jLabel1;
  public javax.swing.JPasswordField txt_pass;
  // End of variables declaration//GEN-END:variables
}
